﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace CandidateManager.App_Code.Helpers
{
    public static class LocalizationHelper
    {
        /// <summary>
        /// Dictionary of languages, used in Resourses
        /// </summary>
        private static readonly Dictionary<String, bool> _languages = new Dictionary<string, bool> {
            {"en-US", true},  // first culture is the DEFAULT
            {"uk-UA", true},
            {"ru-RU", true}
        };

        /// <summary>
        /// Returns cookie with default culture name which is the first name decalared in _languages (e.g. en-US)
        /// </summary>
        /// <returns></returns>
        public static HttpCookie GetDefaultLanguageCookie()
        {
            HttpCookie languageCookie = new HttpCookie("lang");
            languageCookie.Value = _languages.Keys.ElementAt(0); // return Default culture
            languageCookie.Expires = DateTime.Now.AddYears(1);
            return languageCookie;
        }

        /// <summary>
        /// Gets the valid language cookie.
        /// </summary>
        /// <param name="name">The cookie [lang] (lang=ru-RU, for example)</param>
        /// <returns></returns>
        public static HttpCookie GetValidLanguageCookie(HttpCookie name)
        {
            if (string.IsNullOrEmpty(name.Value))
                return GetDefaultLanguageCookie(); // return Default culture

            if (_languages.ContainsKey(name.Value))
                return name;

            return GetDefaultLanguageCookie(); // return Default culture as no match found
        }
        /// <summary>
        /// Gets the valid language cookie.
        /// </summary>
        /// <param name="name">The string (ru-Ru for example).</param>
        /// <returns></returns>
        public static HttpCookie GetValidLanguageCookie(string name)
        {
            if (string.IsNullOrEmpty(name))
                return GetDefaultLanguageCookie(); // return Default culture

            if (_languages.ContainsKey(name))
                return GetNewLanguageCookie(name);

            return GetDefaultLanguageCookie(); // return Default culture as no match found
        }

        private static HttpCookie GetNewLanguageCookie(string name)
        {
            HttpCookie languageCookie = new HttpCookie("lang");
            languageCookie.Value = name;
            languageCookie.Expires = DateTime.Now.AddYears(1);
            return languageCookie;
        }

        public static string GetCurrentCultureDateFormatForJS()
        {
            var currentUICulture = Thread.CurrentThread.CurrentUICulture;

            switch (currentUICulture.Name)
            {
                case "uk-UA":
                case "ru-RU":
                    return "dd.mm.yy";
                default:
                    return "mm/dd/yy";
            }
        }

        public static string GetCurrentCultureDateFormat()
        {
            var currentUICulture = Thread.CurrentThread.CurrentUICulture;

            switch (currentUICulture.Name)
            {
                case "uk-UA":
                case "ru-RU":
                    return "{0:dd.MM.yy}";
                default:
                    return "{0:MM/dd/yy}";
            }
        }
    }
}